<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Menashop
 * @package    Menashop_Content
 * @subpackage Manufacturers
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * The SAX handler for manufacturers
 * 
 * @category   Menashop
 * @package    Menashop_Content
 * @subpackage Manufacturers
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Menashop_Content_Manufacturers
{

    /**
     * The data model for a manufacturer
     * @var Admin_Model_Manufacturer 
     */
    protected $_manufacturerModel = null;

    /**
     * The counter for manufacturers
     * @var integer 
     */
    protected $_totalManufactures = 0;

    /**
     * The counter for inserted manufacturers
     * @var integer 
     */
    protected $_totalManufacturesInserted = 0;

    /**
     * The counter for updated manufacturers
     * @var integer 
     */
    protected $_totalManufacturesUpdated = 0;

    /**
     * Initialize the SAX handler for manufacturers
     */
    public function __construct()
    {
        $this->_manufacturerModel = new Admin_Model_Manufacturer();
    }

    /**
     * Process the start of an element in XML
     * @param obejct $parser
     * @param string $tag
     * @param array $attributes 
     */
    public function startElement($parser, $tag, $attributes)
    {
        if ('Supplier' == $tag) {
            $manufacturer = array();
            // check a sponsor
            if (isset($attributes['Sponsor'])) {
                $manufacturer['content_sponsor'] = 1;
            }
            $manufacturer['name'] = $attributes['Name'];
            $manufacturer['content_id'] = $attributes['ID'];
            // find a manufacturer in the database
            $manufacturerId = $this->_manufacturerModel->findIdByContentId($attributes['ID']);
            if ($manufacturerId) {
                // update the manufacturer
                $manufacturer['id'] = $manufacturerId;
                $this->_totalManufacturesUpdated++;
            } else {
                // insert the manufacturer
                $this->_totalManufacturesInserted++;
            }
            $this->_manufacturerModel->save($manufacturer);
            $this->_totalManufactures++;
        }
    }

    /**
     * Process the end of an element in XML
     * @param object $parser
     * @param string $tag 
     */
    public function endElement($parser, $tag)
    {
        
    }

    /**
     * Process characters in XML
     * @param object $parser
     * @param string $data 
     */
    public function characterData($parser, $data)
    {
        
    }

    /**
     * Get the report about a parsing
     * @return string 
     */
    public function getReport()
    {
        $report = "$this->_totalManufactures manufacturers in XML,";
        $report .= " $this->_totalManufacturesInserted inserted,";
        $report .= " $this->_totalManufacturesUpdated updated";
        return $report;
    }

}

